/**
 * 
 */
package org.framework2.test.webserver.http.script;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.framework2.utils.EncodingDetectUtils;

/**
 * <p>
 * 脚本文件处理器
 * </p>
 * @see 
 * @author ahli
 * @version WebServerTestMock, 2010-8-19
 * @since WebServerTestMockV1
 */
public class ScriptFileHandler {

	/**
	 * 脚本内容
	 */
	private String scriptContent;
	
	/**
	 * 文件路径
	 */
	private String filePath;

	/**
	 * 默认构造函数
	 * @param filePath    文件路径
	 */
	public ScriptFileHandler(String filePath) {
		super();
		this.filePath = filePath;
	}
	
	/**
	 * 读取/解析文件，并加载到内存中
	 * @throws IOException
	 */
	public void readFile() throws IOException{
		File file = new File(filePath);
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), EncodingDetectUtils.getInstance().detectEncoding(file).getName()));
		String tmp = null;
		StringBuilder sb = new StringBuilder();
		while((tmp = br.readLine()) != null){
			sb.append(System.getProperty("line.separator"));
			sb.append(tmp);
		}
		this.scriptContent = sb.toString();
	}
	
	/**
	 * 返回脚本内容
	 * @return
	 */
	public String getContent(){
		return scriptContent;
	}
}
